#ifndef RTPPARSE_TIMER_HANDLE_H
#define RTPPARSE_TIMER_HANDLE_H

#include <uv.h>

class TimerHandle {
public:
    class Listener {
    public:
        virtual ~Listener() = default;

    public:
        virtual void OnTimer(TimerHandle* timerHandle) = 0;
    };

public:
    explicit TimerHandle(Listener* listener);

    TimerHandle& operator=(const TimerHandle&) = delete;
    TimerHandle(const TimerHandle&) = delete;

    ~TimerHandle();

public:
    void Start(uint64_t timeout, uint64_t repeat = 0);
    void Stop();
    void Reset();
    void Restart();

    uint64_t GetTimeout() const {
        return this->timeout;
    }

    uint64_t GetRepeat() const {
        return this->repeat;
    }

    bool IsActive() const {
        return uv_is_active(reinterpret_cast<uv_handle_t*>(this->uv_handle)) != 0;
    }
private:
    void InternalClose();
public:
    void OnUvTimer();

private:
    Listener *listener { nullptr };
    uv_timer_t* uv_handle { nullptr };

    bool closed { false };

    uint64_t timeout { 0u }; // Timeout in milliseconds.
    uint64_t repeat { 0u };  // Repeat interval in milliseconds.
};


#endif